package com.cnb.telepathytalk;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.cnb.telepathytalk.fragment.Tab0_Fragment;
import com.cnb.telepathytalk.fragment.Tab1_Fragment;
import com.cnb.telepathytalk.fragment.Tab2_Fragment;
import com.cnb.telepathytalk.fragment.Tab3_Fragment;
import com.cnb.telepathytalk.fragment.Tab4_Fragment;

public class MainActivity extends DefaultActivity implements View.OnClickListener {
	private static final int TAB_SIZE = 5;
	public static int nowTab = 2;

	private Fragment[] tabFragments = new Fragment[TAB_SIZE];
	private View tabButtons[] = new View[TAB_SIZE];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tabFragments[0] = new Tab0_Fragment();
		tabFragments[1] = new Tab1_Fragment();
		tabFragments[2] = new Tab2_Fragment();
		tabFragments[3] = new Tab3_Fragment();
		tabFragments[4] = new Tab4_Fragment();

		tabButtons[0] = findViewById(R.id.main_tab0);
		tabButtons[1] = findViewById(R.id.main_tab1);
		tabButtons[2] = findViewById(R.id.main_tab2);
		tabButtons[3] = findViewById(R.id.main_tab3);
		tabButtons[4] = findViewById(R.id.main_tab4);

		for (int i = 0; i < TAB_SIZE; i++)
			tabButtons[i].setOnClickListener(this);

		setTab(nowTab);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_tab0:
			setTab(0);
			break;
		case R.id.main_tab1:
			setTab(1);
			break;
		case R.id.main_tab2:
			setTab(2);
			break;
		case R.id.main_tab3:
			setTab(3);
			break;
		case R.id.main_tab4:
			setTab(4);
			break;
		}
	}

	private void setTab(int position) {
		nowTab = position;

		for (int i = 0; i < TAB_SIZE; i++) {
			if (i == position) {
				tabButtons[i].setSelected(true);
				getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment, tabFragments[position]).commit();
			} else {
				tabButtons[i].setSelected(false);
			}
		}
	}
}